package com.hetao.xx.service;

import com.hetao.xx.Global;
import com.hetao.xx.constants;
import com.hetao.xx.game.model.NpcEvent;
import com.hetao.xx.orm.NpcEventVo;
import com.hetao.xx.orm.NpcVo;
import com.hetao.xx.orm.PlaceVo;

import java.util.ArrayList;
import java.util.Random;

public class PlaceEventService {


    public static void clickPlace(PlaceVo place) {
//        NpcEvent event = randomNpcEvent(place.events);
//        if(event == null) {
//            Global.getInstant().notifyMsg("无事发生，天谴吗");
//            return;
//        }

        Global.getInstant().switchMainPage(constants.PlacePage, 0, place);

    }

    public static void clickNpc(NpcVo npc) {
        Global.getInstant().switchMainPage(constants.NpcPage, 0, npc);
    }

    public static void clickEvent(NpcEventVo eventVo) {
        Global.getInstant().switchMainPage(constants.EventPage, 0, eventVo);
    }

    public static void clickCancelEvent(NpcEventVo eventVo) {
        Global.getInstant().switchMainPage(constants.NpcPage, eventVo.npcId, null);
    }

    public static void backPlace(PlaceVo place) {
        Global.getInstant().switchMainPage(constants.PlacePage, place.prePlaceId, null);
    }

    public static void backNpc(NpcVo npc) {
        Global.getInstant().switchMainPage(constants.PlacePage, npc.placeId, null);
    }

//    public static NpcEvent randomNpcEvent(List<NpcEvent> npcEvents) {
//        if (UtilList.isEmpty(npcEvents)) {
//            return null;
//        }
//
//        ArrayList<Integer> tmpList = new ArrayList<>(4);
//        int limit=0;
//        for (NpcEvent event : npcEvents) {
//            limit+=event.present;
//            tmpList.add(limit);
//        }
//
//        int index = getIndexByRandom(tmpList);
//        if (index == npcEvents.size()) {
//            return null;
//        }
//        return npcEvents.get(index);
//    }

    public static int getIndexByRandom(ArrayList<Integer> tmpList) {
        int random = new Random().nextInt(100) + 1;
        for (int i=0;i<tmpList.size();i++) {
            Integer tmp = tmpList.get(i);
            if (random <= tmp) {
                return i;
            }
        }
        return tmpList.size();
    }

    public static boolean clickNpcEventOk(NpcEvent event) {
        switch (event.okEvent) {
            case Talk:
                break;
            case SaveArchive:
//                UserManager.saveUserInfo(Global.getInstant().getUser().info);
                Global.getInstant().notifyMsg("已保存进度");
                break;
            case Fight:
                break;
            case GiveTo:
                break;
        }
        return true;
    }

    public static boolean clickNpcEventCancel(NpcEvent event) {
        return true;
    }

    public static boolean backPage(NpcEvent event) {
//        Place place = Global.getInstant().cityManager.getCurPlace();
//        if(place.prePlace == null) {
//            //城市下第一步地点
//            Global.getInstant().cityManager.setCurPlace(null);
//            Global.getInstant().switchMainPage(constants.CityPage, Global.getInstant().cityManager.getCurCity());
//        } else {
//            Global.getInstant().cityManager.setCurPlace(place.prePlace);
//            Global.getInstant().switchMainPage(place.prePlace., Global.getInstant().cityManager.getCurCity());
//        }
        return true;
    }
}
